package com.sunducation.wts.penbox.presenter;

import android.content.Context;

import com.google.gson.Gson;
import com.sunducation.wts.penbox.contract.HomeContract;
import com.wetao.lib_common.base.BaseResponse;
import com.wetao.lib_common.bean.response.AppVersionInfo;
import com.wetao.lib_common.bean.response.ReadPushListInfo;
import com.wetao.lib_common.bean.response.StudentExamListInfo;
import com.wetao.lib_common.bean.response.StudentLiveMessageInfo;
import com.wetao.lib_common.bean.response.StudyTaskCountInfo;
import com.wetao.lib_common.net.RetryWithDelay;
import com.wetao.lib_common.net.RxUtils;
import com.wetao.lib_common.net.ServerUtils;
import com.wetao.lib_common.net.callback.RequestCallback;
import com.wetao.lib_common.net.callback.RxErrorHandler;
import com.wetao.lib_common.utils.JsonUtils;
import com.wetao.lib_common.utils.LogUtils;
import com.wetao.lib_common.utils.ToastUtil;

import java.util.HashMap;

import okhttp3.RequestBody;

/**
 * author: zhouyh
 * created on: 3/8/21 11:51 AM
 * description:
 */
public class HomePresenter extends HomeContract.Presenter {


    private Context mContext;

    public HomePresenter(Context mContext) {
        this.mContext = mContext;
    }


    @Override
    public void getHomeStudentExamList(HashMap<String, Object> map, boolean isDialog, boolean cancelable) {
        if (getView()==null)return;
        String json = (JsonUtils.getmGson()).toJson(map);
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), json);
        ServerUtils.getCommonApi().getHomeStudentExamList(requestBody)
                .retryWhen(new RetryWithDelay(2, 1))
                .compose(RxUtils.<StudentExamListInfo>bindToLifecycle(getView()))
                .compose(RxUtils.<StudentExamListInfo>getSchedulerTransformer())
                .subscribe(new RequestCallback<StudentExamListInfo>(mContext, RxErrorHandler.getInstance(), isDialog, cancelable) {
                    @Override
                    public void onNext(StudentExamListInfo baseResponse) {
                        super.onNext(baseResponse);
                        if (baseResponse.getCode() == 0) {
                            if (getView() != null) {
                                getView().getHomeStudentExamListResult(baseResponse);
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        getView().getHomeStudentExamListFail();
                    }
                });
    }

    @Override
    public void getHomePageCount(boolean isDialog, boolean cancelable) {
        if (getView()==null)return;
        ServerUtils.getCommonApi().getHomePageCount()
                .retryWhen(new RetryWithDelay(2, 1))
                .compose(RxUtils.<StudyTaskCountInfo>bindToLifecycle(getView()))
                .compose(RxUtils.<StudyTaskCountInfo>getSchedulerTransformer())
                .subscribe(new RequestCallback<StudyTaskCountInfo>(mContext, RxErrorHandler.getInstance(), isDialog, cancelable) {
                    @Override
                    public void onNext(StudyTaskCountInfo baseResponse) {
                        super.onNext(baseResponse);
                        if (baseResponse.getCode() == 0) {
                            if (getView() != null) {
                                getView().getStudyTaskCountResult(baseResponse);
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (getView() != null) {
                            getView().getStudyTaskCountFail();
                        }
                    }
                });
    }

    @Override
    public void getStudentLiveMessageList(HashMap<String, Object> map,boolean isDialog, boolean cancelable) {
        if (getView()==null)return;
        String json = (JsonUtils.getmGson()).toJson(map);
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), json);
        ServerUtils.getCommonApi().getHomeStudentLiveMessageList(requestBody)
                .retryWhen(new RetryWithDelay(2, 1))
                .compose(RxUtils.<StudentLiveMessageInfo>bindToLifecycle(getView()))
                .compose(RxUtils.<StudentLiveMessageInfo>getSchedulerTransformer())
                .subscribe(new RequestCallback<StudentLiveMessageInfo>(mContext, RxErrorHandler.getInstance(), isDialog, cancelable) {
                    @Override
                    public void onNext(StudentLiveMessageInfo baseResponse) {
                        super.onNext(baseResponse);
                        if (baseResponse.getCode() == 0) {
                            if (getView() != null) {
                                getView().getStudentLiveMessageListResult(baseResponse);
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (getView() != null) {
                            getView().getStudentLiveMessageListFail();
                        }
                    }
                });
    }

    @Override
    public void getClientVersionInfo(HashMap<String, Object> map, boolean isDialog, boolean cancelable) {
        if (getView()==null)return;
        String json = (JsonUtils.getmGson()).toJson(map);
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), json);
        ServerUtils.getCommonApi().getStudentScreenClientVersion(requestBody)
                .retryWhen(new RetryWithDelay(2, 1))
                .compose(RxUtils.<BaseResponse<AppVersionInfo>>bindToLifecycle(getView()))
                .compose(RxUtils.<BaseResponse<AppVersionInfo>>getSchedulerTransformer())
                .subscribe(new RequestCallback<BaseResponse<AppVersionInfo>>(mContext, RxErrorHandler.getInstance(), isDialog, cancelable) {
                    @Override
                    public void onNext(BaseResponse<AppVersionInfo> baseResponse) {
                        super.onNext(baseResponse);
                        if(baseResponse.getCode() == 0){
                            getView().getClientVersionResult(baseResponse);
                        }else {
                            getView().getClientVersionError();
                            ToastUtil.initToast(baseResponse.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        getView().getClientVersionError();
                        super.onError(e);
                    }
                });
    }


    @Override
    public void getCommitAnswer(String json, boolean isDialog, String tip, boolean isCancelable) {
        if (getView()==null)return;
        LogUtils.d("req json = " + json);
        RequestBody requestBody =  RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        ServerUtils.getCommonApi().commitAnswer(requestBody)
                .retryWhen(new RetryWithDelay(3,2))
                .compose(RxUtils.<BaseResponse>bindToLifecycle(getView()))
                .compose(RxUtils.<BaseResponse>getSchedulerTransformer())
                .subscribe(new RequestCallback<BaseResponse>(mContext, RxErrorHandler.getInstance(),isDialog, tip, isCancelable) {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        super.onNext(baseResponse);
                        if(baseResponse.getCode() == 0){
                            getView().getCommitAnswerResult(baseResponse);
                        }else{
                            ToastUtil.initToast(baseResponse.getMsg());
                            getView().getCommitAnswerFailed();
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        //ToastUtil.initToast(mContext.getString(R.string.network_disconnect));
                        getView().getCommitAnswerFailed();
                    }
                });
    }

    @Override
    public void getReadPushList(HashMap<String, String> map, boolean isDialog, boolean isCancelable) {
        if (getView()==null)return;
        String json =(new Gson()).toJson(map);
        RequestBody requestBody =  RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        ServerUtils.getCommonApi().getStudentReadPushList(requestBody)
                .retryWhen(new RetryWithDelay(3,2))
                .compose(RxUtils.<ReadPushListInfo>bindToLifecycle(getView()))
                .compose(RxUtils.<ReadPushListInfo>getSchedulerTransformer())
                .subscribe(new RequestCallback<ReadPushListInfo>(mContext, RxErrorHandler.getInstance(),isDialog,isCancelable) {
                    @Override
                    public void onNext(ReadPushListInfo baseResponse) {
                        super.onNext(baseResponse);
                        if(baseResponse.getCode() == 0){
                            getView().getReadPushListResult(baseResponse);
                        }else{
                            ToastUtil.initToast(baseResponse.getMsg());
                            getView().getReadPushListFailed();
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        getView().getReadPushListFailed();
                    }
                });
    }


}
